﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour
{
    public static ObjectPool Instance;
    private int initSpawnCount = 100;
    private List<GameObject> normalPlatformList = new List<GameObject>();
    private ManagerVars vars;

    private void Awake()
    {
        Instance = this;
        vars = ManagerVars.GetManagerVars();
        Init();
    }

    private void Init()
    {
        for (int i = 0;i<initSpawnCount;i++)
        {
            normalPlatformList.Add(InstantiateObject(vars.normalPlatformPre, ref normalPlatformList));
        }
    }

    public GameObject GetNormalPlatform()
    {
        for (int i = 0; i < normalPlatformList.Count; i++)
        {
            if (normalPlatformList[i].activeInHierarchy == false)
            {
                return normalPlatformList[i];
            }
        }
       
        return InstantiateObject(vars.normalPlatformPre,ref normalPlatformList);
    }

    public static int Index = 0;


    public GameObject GetActivePlatform()
    {
        return normalPlatformList[Index++];
    }

    private GameObject InstantiateObject(GameObject pre,ref List<GameObject> list)
    {
        GameObject go = Instantiate(pre, transform);
        go.SetActive(false);
        list.Add(go);
        return go;
    }
}
